package cn.tjie.org.controller;

import cn.tjie.basic.annotation.PreAuthorize;
import cn.tjie.basic.util.AjaxResult;
import cn.tjie.basic.util.PageList;
import cn.tjie.org.domain.Employee;
import cn.tjie.org.query.EmployeeQuery;
import cn.tjie.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@RestController //@Controller +@ResponseBody
@RequestMapping("/employee")
@Api(value ="部门接口类" ,description ="处理部门CRUD操作的")
public class EmpployeeController {

    //自动注入
    @Autowired
    private EmployeeService employeeService;

    /**
     * 接口：查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "接口：查询单个对象")
    @PreAuthorize(name = "查询单个员工",sn = "employee:getOne") //value后面用来做权限校验@PreAuthorize(Value=)
    public Employee findOne(@PathVariable("id")Long id){
        return employeeService.loadById(id);
    }

    /**
     * 接口：查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "接口：查询所有对象")
    @PreAuthorize(name = "查询所有员工",sn = "employee:getAll")
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "接口：删除单个对象")
    @PreAuthorize(name = "删除单个员工",sn = "employee:delete")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }

    }

    /**
     * 接口：添加和修改
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "接口：添加和修改单个对象")
    @PreAuthorize(name = "保存员工",sn = "employee:save")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                //没有获取到id 表示为添加
                employeeService.save(employee);
            }else {
                //有id为修改
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：分页和高级查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "接口：分页和高级查询")
    @PreAuthorize(name = "员工列表",sn = "employee:list")
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.pageQuery(employeeQuery);
    }


    @PatchMapping
    @ApiOperation(value = "接口：批量删除")
    @PreAuthorize(name = "批量删除员工",sn = "employee:patchDel")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }

    }

    /**
     * 接口：
     * @return
     */
    @GetMapping("/list/{shopId}")
    @ApiOperation(value = "接口：查询所有对象")
    @PreAuthorize(name = "查询所有员工",sn = "employee:getAll")
    public List<Employee> handleAccept(@PathVariable("shopId")Long id){
        return employeeService.handleAccept(id);
    }

}
